import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class Delete extends JFrame 
{
    ArrayList<String> values = new ArrayList<String>();// The selected values.

    private String selected = "";

    private JTextField displayField = new JTextField(30);

    JFrame frame = new JFrame();

    private JComboBox c = new JComboBox();

    private JButton b = new JButton("Add item");


    private int count = 0, p = 0;;
    String manuf ="" ;
    String model = "", year= "", description = "",litres, inches= "", engine= "";

    ArrayList<String> temp;
    //ArrayList<String> temp2;
    private Connection con;
    private ConnectDb d;
    private String[] crxFields = {"ARS1",
            "ARS2",
            "ARS3",
            "ARS4",
            "MOD1",
            "MOD2",
            "MOD3",
            "MOD4",
            "BEH1",
            "BEH2",
            "BEH3",
            "BEH4",
            "DAN1",
            "DAN2",
            "DAN3",
    "DAN4"};
    
    public Delete(Connection con, ConnectDb d) 
    {
        this.con = con;
        this.d = d;
        //-------------------------------------------------------------------
        final Query q = new Query(con, d);

        final JPanel displayPanel = new JPanel() ; // the upper panel.
        final JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.

        String option = JOptionPane.showInputDialog("Press P for delete part \n Press O to delete other");

        if(option.equalsIgnoreCase("P"))
        {
            String pnum = JOptionPane.showInputDialog("Enter the part number you would like to update: ");
            String newPnum = "NS";

            String query ="";
            //check if part number exists

            for(int i = 0; i <4; i++)
            {
                String tableName = "";
                if(i==0)
                    tableName = "RDIMARS";
                else if (i==1)
                    tableName = "RDIMBEH";
                else if (i==2)
                    tableName = "RDIMDAN";
                else
                    tableName = "RDIMMOD";

                updatePnum(pnum,newPnum,tableName);
            }
        }
        else
        {
        
        
        temp  = q.queryManufac();
        int x = temp.size();
        p++;
        for(int i = 0;i<x;i++)
            c.addItem(temp.get(i));
        //----------------------------------------
        displayField.setEditable(false);
        

        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {                       

                selected = (String) c.getSelectedItem();
                System.out.println(selected);
                if(selected == null)
                {
                    selected = "";
                }

                if(count == 0)
                {
                    temp = q.queryModel(selected.trim());

                }
                else if(count == 1)
                {
                    temp = q.queryYear(selected.trim());
                }
                else if(count == 2)
                {
                    temp = q.queryLitres(selected.trim());
                    printList(temp);
                }
                else if(count == 3)
                {
                    System.out.println("selected:" + selected);
                    temp = q.queryEngineType(selected.trim());
                    printList(temp);

                }
                else if(count == 4)
                {
                    temp = q.queryCubicInches(selected.trim());
                }
                else if(count == 5)
                {
                    temp = q.queryDesc(selected.trim());

                }
                else if(count == 6)
                {

                    temp = q.queryRlink(selected.trim());
                }
                else if(count == 7)
                {

                        temp = q.queryPNum(temp.get(0));

                        
                    
                }
                else if (count == 8)
                {

                    displayPanel.setVisible(false);
                    optionPanel.setVisible(false);
                    JPanel panel = new JPanel();

                    JButton button = new JButton("Cancel");
                    button.addActionListener(new ActionListener()
                    {
                        
                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            frame.setVisible(false);
                        }
                    });
                    JButton button2 = new JButton("Delete");
                    button2.addActionListener(new ActionListener()
                    {
                        
                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            q.delete();
                            frame.setVisible(false);
                        }
                    });
                    panel.add(button);
                    panel.add(button2);
                    panel.setVisible(true);
                    frame.add(panel);
                    frame.repaint();
                    /*
                    displayPanel.setVisible(false);
                    optionPanel.setVisible(false);
                    JPanel panel = new JPanel();

                    ArrayList<String> parts = q.queryPNum(temp.get(0));
                    printList(parts);

                    JList jlist = new JList(parts.toArray());
                    JScrollPane pane = new JScrollPane(jlist);

                    panel.add(pane);
                    panel.setVisible(true);
                    frame.add(panel);
                    frame.repaint();
                    */
                }
                
                c.removeAllItems();

                if (temp == null || temp.isEmpty() || temp.equals(null))
                    frame.setVisible(false);
                else
                for(int i = 0; i < temp.size(); i++)
                {
                    c.addItem(temp.get(i));
                }

                count++;
            }
        });
        }





        optionPanel.setLayout(new FlowLayout());
        displayPanel.add(displayField);
        optionPanel.add(c);
        optionPanel.add(b);
        //connectDb.disconnectFromDB();

        frame.setLayout(new BorderLayout());
        frame.add(displayPanel,BorderLayout.NORTH);
        frame.add(optionPanel,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);

    }

    public void printList(ArrayList<String> a)
    {
        for(int i = 0; i < a.size(); i++)
            System.out.println("LIST:" + a.get(i));
    }
    
    private void updatePnum(String pnum, String newPnum, String tname) 
    {
        newPnum = "NS";
        //String update = "UPDATE "+tname+" SET p_number = "+newPnum+" WHERE p_number = "+pnum+";";
        String update = "UPDATE "+tname+" SET p_number = '"+newPnum+"' WHERE p_number = '" + pnum + "'";


        System.out.println("neiw 141"+newPnum +" --"+update);

        //String updateAll = "UPDATE radcrx set "
        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update

            //con.commit();
        }       
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }

        for (int i = 0; i < crxFields.length; i++)
        {
            String updateCrx = "UPDATE radcrx set " + crxFields[i] + "='" + newPnum + "' where " + crxFields[i] + "='" + pnum+"'"; 

            try
            {          
                Statement stmnt2 = con.createStatement();
                int rs2 = stmnt2.executeUpdate(updateCrx); // update

                //JOptionPane.showMessageDialog(frame, "Update Success!");
            }   
            catch (SQLException ex) 
            {
                System.err.println(ex.getMessage());
            }
        }

        //done with update, commit
        try
        {
            con.commit();
        }
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }




}

